package com.bardpub.weblite.connect 
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * Copyright © 2010-2012, BardPub.com
	 * @fileName 
	 * @author LittleBard
	 * @version 1.0
	 * @date 
	 * @descrition 
	 * @others
	 * @history
	 */
	
	public class BasicRequest extends EventDispatcher
	{

		private var __url:String;
		private var __format:String;
		private var __tag:WebRequestTag;
		private var __data:*;
		private var __isLoading:Boolean = false;
		private var __txtLoader:URLLoader;
		private var __objLoader:Loader;
		
		/**
		 * 创建 com.bardpub.weblite.connect.BasicRequest 类的新实例
		 * @param	url  资源URL
		 * @param	format	资源格式
		 */
		public function BasicRequest( tag:WebRequestTag) 
		{
			__tag = tag;
			__url = tag.url;
			__format = tag.format;
			
		}
		
		/**
		 * 获取data
		 */
		public function get data():*
		{
			return __data;
		}
		
		/**
		 * 获取/设置tag
		 */
		public function get tag():WebRequestTag
		{
			return __tag;
		}
		public function set tag(value:WebRequestTag):void 
		{
			__tag == null;
			__tag = value.clone();
			__url = value.url;
			__format = value.format;
		}
		
		
		/**
		 * 初始化
		 */
		public function init():void 
		{

			
			if (__format == WebRequest.TYPE_JSON || 
			    __format == WebRequest.TYPE_TXT || 
				__format == WebRequest.TYPE_XML)
			{
				if (__isLoading)
				{
					__txtLoader.close();
					removeHandler();
				}
				__txtLoader = new URLLoader();
				__txtLoader.load(new URLRequest(__url));
				__txtLoader.addEventListener(Event.COMPLETE, onLoadComplete);
				
			}
			else
			{
				if (__isLoading)
				{
					__objLoader.unload();
					removeHandler();
				}
				
				__objLoader = new Loader();
				__objLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
				__objLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
				__objLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
				__objLoader.load(new URLRequest(__url));
			}
			__isLoading = true;
		}
		
		/**
		 * 关闭已打开的链接
		 */
		public function close():void 
		{
			
			if (__isLoading)
			{
				removeHandler();
			}
		}
		
		private function removeHandler():void 
		{
			
			__isLoading = false;
			if (__format == WebRequest.TYPE_JSON || 
			    __format == WebRequest.TYPE_TXT || 
				__format == WebRequest.TYPE_XML)
			{
				__txtLoader.removeEventListener(Event.COMPLETE, onLoadComplete);
				__txtLoader = null;
			}
			else
			{
				__objLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
				__objLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoadProgress);
				__objLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
				__objLoader = null;
			}
		}
		
		/**
		 * 当发生IO错误时触发
		 * @param	e
		 */
		private function onIOError(e:IOErrorEvent):void 
		{
			
			dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
			if (tag.errorHandler != null)
			{
				tag.errorHandler.apply(null, null);
			}
			
			removeHandler();
		}
		
		/**
		 * 当载入进度变化时发生
		 * @param	e
		 */
		private function onLoadProgress(e:ProgressEvent):void 
		{
			if (tag.progressHandler != null)
			{
				tag.progressHandler.apply(null, [e.bytesLoaded, e.bytesTotal]);
			}
		}
		
		/**
		 * 当载入完成时发生
		 * @param	e
		 */
		private function onLoadComplete(e:Event):void 
		{		
			
			
			if (__format == WebRequest.TYPE_JSON || 
			    __format == WebRequest.TYPE_TXT)
			{
				__data = URLLoader(e.target).data;
				
			}
			else if (__format == WebRequest.TYPE_XML)
			{
				__data = XML(URLLoader(e.target).data);
			}
			else 
			{
				__data = LoaderInfo(e.target).content;
			}
				
			if (__tag.successHandler != null)
			{
				__tag.successHandler.apply(null, [__data]);
			}
			dispatchEvent(new Event(Event.COMPLETE));
		}

	}

}